package view;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.zip.Inflater;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.devgroupvn.dota.Application;
import com.devgroupvn.dota.Entry;
import com.devgroupvn.dota.Post;
import com.devgroupvn.dota.PostAdapter;
import com.devgroupvn.dota.ShareSNS;
import com.devgroupvn.dota.ViewPagerActivity;
import com.devgroupvn.dota.ViewPost;
import com.devgroupvn.dota.WebContent;

public class ListView1Page extends LinearLayout
{

	public static List<Entry> listData = new LinkedList<Entry>();
	private Context mContext;
	private Activity mActivity;
	Inflater inflater;
	LayoutInflater layoutInflater;
	PostAdapter adapter;
	ListView mListView;
	private Asyncx async;

	private static int index = 1;
	private static String curcat = "";
	private static String[] mValText;

	public ListView1Page(Context context, AttributeSet attrs)
	{
		super(context, attrs);

		// init();
	}

	public ListView1Page(Context context, Activity _Activity)
	{
		super(context);
		mContext = context;
		 mActivity = _Activity;
		mListView = new ListView(mContext);
		init();
	}

	private void init()
	{
		LayoutParams params = new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT);
		addView(mListView, params);
		loadmore();
		adapter = new PostAdapter(mActivity, listData);
		mWebContent = new WebContent();
		action();
		progressDialog = ProgressDialog.show(mContext, null, "Waiting..", true, false);
		mListView.setAdapter(adapter);

		async = new Asyncx("http://www.joindota.com/en/vods", adapter);
		async.url = "http://www.joindota.com/en/vods";
		async.execute();

	}

	public static int page = 0;
	private static int pagetam = 0;
	public static ProgressDialog progressDialog;

	private void loadmore()
	{
		mListView.setOnScrollListener(new OnScrollListener()
		{

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState)
			{

			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
			{
				if (totalItemCount == 0) { return; }
				if (!isNetworkAvailable()) { return; }
				int lastitem = firstVisibleItem + visibleItemCount;
				if (lastitem == totalItemCount && page > pagetam && page < 31)
				{
					pagetam = page;
					async = new Asyncx("http://www.joindota.com/en/vods", adapter);
					async.url = "http://www.joindota.com/en/vods/&archiv_page=" + page;
					async.execute();
				}
			}
		});
	}

	private void action()
	{
		/** on item click event for list view */
		mListView.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> a, View v, int position, long id)
			{
				if (!isNetworkAvailable()) { return; }
//				Intent intent = new Intent(v.getContext(),ViewPost.class);
//				intent.putExtra("link",listData.get(position).mLink);
				ViewPagerActivity.strLayout= ViewPagerActivity.LAYOUT_SHARE;
				Application.getInstance().putData("Entry", listData.get(position));
				Intent intent = new Intent(mActivity,ShareSNS.class);
				mActivity.startActivity(intent);

			}
		});
	}

	private boolean isNetworkAvailable()
	{

		ConnectivityManager connMgr = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = connMgr.getActiveNetworkInfo();
		return (info != null && info.isConnected());
	}

	private static WebContent mWebContent;
	private static ArrayList<Post> listpost;

	public class Asyncx extends AsyncTask<Void, Void, Void>
	{

		public String url;

		public Asyncx(String _url, PostAdapter _adapter)
		{
			url = _url;
		}

		@Override
		protected Void doInBackground(Void... arg0)
		{
			loadPostList(url);
			return null;
		}

		@Override
		protected void onPostExecute(Void result)
		{
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			page++;
			adapter.notifyDataSetChanged();
			if (progressDialog.isShowing())
			{
				progressDialog.dismiss();
			}
		}

		private void loadPostList(String url)
		{
			if (url == null)
				return;

			// ViewPagerActivity.curcat = url;
			curcat = url;

			try
			{
				listpost = mWebContent.getPostList(url);
				// +
				// "/page/"
				// +
				// VideoDota2Activity.index
				// +
				// "/"
				Log.d("mydebug3", "troi oi");
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}

			Entry _entry;
			String des;
			String link;
			String image;
			String date;
			String title;
			mValText = new String[listpost.size()];
			int i = 0;
			for (Post post : listpost)
			{
				des = post.title;
				link = post.link;
				image = post.img;
				title = post.title;
				date = post.date;
				i = i + 1;
				_entry = new Entry(des, image, link);
				_entry.title = title;
				_entry.date = date;
				listData.add(_entry);
				Log.d("mydebug3", "~~~" + des);
			}
		}
	}
}
